<?php
namespace Admin\Controller;
use Think\Controller;

class ReportController extends CommonController
{
    public $begin;
    public $end;
    public function _initialize()
    {
        $params = array();
        parent::_initialize($params);
        $this->assign("now_module" ,"report");
        $starttime = I('start_time');
        $endtime = I('end_time');
        if($starttime&&$endtime){
            $begin = $starttime;
            $end = $endtime;
        }else{
            $lastyear = date('Y-m-d h:i:s',strtotime("-3 month"));//30天前
            $begin = I('begin',$lastyear);
            $end =  I('end',date('Y-m-d h:i:s'));
        }
//        $this->assign('start_time',$begin);
//        $this->assign('end_time',$end);
        $this->begin = strtotime($begin);
        $this->end = strtotime($end);
        $this->assign('start_time',date('Y-m-d h:i:s',$this->begin));
        $this->assign('end_time',date('Y-m-d h:i:s',$this->end));
    }

        // 订单统计
        public function order()
    {
        $st = strtotime(date("Y-m-01"));
        $et = time();
        $where = "`time` >= '$st' AND `time` <= '$et'";
        if(isset($_REQUEST['st']))
        {
            $st = strtotime(trim(I('st')));
            $et = strtotime(trim(I('et')) ." 23:59:59");
            if($st != ''){ $where = "`time` >= '$st'"; }
            if($et != ''){ $where = "`time` <= '$et'"; }
            if($st != '' && $et != ''){ $where = "`time` >= '$st' AND `time` <= '$et'"; }
        }

        $complete_where = $where . " AND `status` = 3";
        $this->complete_orders = M("order")->where($complete_where)->count();  // 已完成  3
        $complete_amts = M("order")->where($complete_where)->sum("price");  // 已完成  3
        $confirm_where = $where . " AND `status` = 2";
        $this->confirm_orders = M("order")->where($confirm_where)->count();  // 已确认  2
        $confirm_amts = M("order")->where($confirm_where)->sum("price");  // 已确认  2
        $for_confirm_where = $where . " AND `status` = 1";
        $this->for_confirm_orders = M("order")->where($for_confirm_where)->count();  // 待确认  1
        $for_confirm_amts = M("order")->where($for_confirm_where)->sum("price");  // 待确认  1
        $calcel_where = $where . " AND `status` = '-1'";
        $this->calcel_orders = M("order")->where($calcel_where)->count();  // 已取消  -1
        $calcel_amts = M("order")->where($calcel_where)->sum("price");  // 已取消  -1
        $this->st = date("Y-m-d" ,$st);
        $this->et = date("Y-m-d" ,$et);

        $this->complete_amts = $complete_amts ? $complete_amts : 0;
        $this->confirm_amts = $confirm_amts ? $confirm_amts : 0;
        $this->for_confirm_amts = $for_confirm_amts ? $for_confirm_amts : 0;
        $this->calcel_amts = $calcel_amts ? $calcel_amts : 0;
        $amt_count = M("order")->where("`status` = 3")->sum("price");
        $this->amt_count = $amt_count ? $amt_count : 0;
        $this->display();
    }
        //报表统计
        public function sales_profile(){
            $Model = new \Think\Model();
            //今天九点开始
            $now = strtotime(date('Y-m-d'));
            $today['today_amount'] = M('order')->where("time>$now AND status = 3")->sum('price');//今日销售总额
            $today['today_order'] = M('order')->where("time>$now AND status = 3")->count();//今日订单数
            $today['cancel_order'] = M('order')->where("time>$now AND status = -1")->count();//今日取消订单
            $today['sign'] = round($today['today_amount']/$today['today_order'],2);
            $this->assign('today',$today);
            $sql = "SELECT COUNT(*) as tnum,sum(price) as amount, FROM_UNIXTIME(time,'%Y-%m-%d') as gap from  yd_order ";
            $sql .= " where time>$this->begin and time<$this->end AND status=3  group by gap ";
            $res = $Model->query($sql);//订单数,交易额

//            一年的订单数交易额
            foreach ($res as $val){
                $arr[$val['gap']] = $val['tnum'];
                $brr[$val['gap']] = $val['amount'];
                $tnum += $val['tnum'];
                $tamount += $val['amount'];
            }

            for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
                $tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
                $tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
                $tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);
                $order_arr[] = $tmp_num;
                $amount_arr[] = $tmp_amount;
                $sign_arr[] = $tmp_sign;
                $date = date('Y-m-d',$i);
                $list[] = array('day'=>$date,
                          'order_num'=>$tmp_num,
                             'amount'=>$tmp_amount,
                               'sign'=>$tmp_sign,
                                'end'=>date('Y-m-d',$i+24*60*60));
                $day[] = $date;
            }
            //反键
            rsort($list);
            $res = array_page($list);
            $show = $res['page']->show();
            $this->assign('page',$show);
            $this->assign('list',$res['arr']);
            $result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day);
            $this->assign('result',json_encode($result));
            $this->display();
        }

        //商品排行
        public function  hot_ranking(){
            $Model = new \Admin\Model\OrderGoodViewModel();
            //分类列表
            $categories = M('category')->where(array('status' => 1))->order('sort ASC')->select();
            $category = sortCate($categories);
            //品牌列表
            $brand = M('brand')->where(array('status'=>1))->order('id DESC')->select();
            if(IS_AJAX){
                $cat_id = I("post.cat_id");
                $brand_id = I("post.brand_id");
                $start_time = I("post.start_time");
                $end_time = I("post.end_time");
                $orderby = I("post.orderby");
                if(!$start_time){
                    $start_time = $this->begin;
                }else{
                    $start_time = strtotime($start_time);
                }
                if(!$end_time){
                    $end_time = $this->end;
                }else{
                    $end_time = strtotime($end_time);
                }
                if(!$orderby){
                    $orderby = "sumnum DESC";
                }
//                实例化模型grouby
                $res=$Model->where("$start_time < time AND $end_time > time")
                           ->field('good_id,time,sum(goods_price) as sumprice,sum(num) as sumnum')
                           ->group('good_id')->limit(50)->order($orderby)->select();
                if(!empty($cat_id)){
                    $where['cate'] = $cat_id;
                }
                if(!empty($brand_id)){
                    $where['brand_id'] = $brand_id;
                }
                //筛选是否符合条件
                foreach ($res as $key => $value){
                   $where['id'] = $value['good_id'];
                    $result = M('good')->where($where)->find();
                   if(!$result){
                       unset($res[$key]);
                   }else{
                       $res[$key]['good_name'] = $result['g_name'];
                   }
                }
                //重新进行排序
                $res = resort($res);
                $this->assign('res',$res);
                $this->display('hot_query');
            }else{
//                $year = date("Y");
//                $month = date("m");
//                $day = date("d");
//                $start_time = mktime(0,0,0,$month,$day,$year);//当天开始时间戳
//                $end_time= mktime(23,59,59,$month,$day,$year);//当天结束时间戳

                $res=$Model->where("$this->begin < time AND $this->end > time")
                           ->field('good_id,time,sum(goods_price) as sumprice,sum(num) as sumnum')
                           ->group('good_id')->limit(50)->order('sumnum DESC')->select();
                foreach ($res as $key => $value){
                    $good_name = M('good')->where(array('id'=>$value['good_id']))->getField('g_name');
                    $value['good_name'] = $good_name;
                    $res[$key] = $value;
                }
                $this->assign('categories', $category);
                $this->assign('brands', $brand);
                $this->assign('res',$res);
                $this->display();
            }

        }

        //代理排行
        public function agent_ranking(){
            $Model = new \Admin\Model\OrderViewModel();

            if(IS_AJAX){
                $start_time = I("post.start_time");
                $end_time = I("post.end_time");
                $orderby = I("post.orderby");
                if(!$start_time){
                    $start_time = $this->begin;
                }
                if(!$end_time){
                    $end_time = $this->end;
                }
                if(!$orderby){
                    $orderby = "sumnum DESC";
                }
                $res=$Model->where("$start_time < time AND $end_time > time")
                           ->field('count(*) as sumnum,agent_id,time,agent_name,sum(price) as sumprice,')
                           ->group('agent_id')
                           ->limit(50)
                           ->order($orderby)->select();
                $this->assign('res',$res);
                $this->display('agent_query');
            }else{
                $res=$Model
                    ->where("$this->begin < time AND $this->end > time")
                    ->field('count(*) as sumnum,agent_id,time,agent_name,sum(price) as sumprice,')
                    ->group('agent_id')
                    ->limit(50)
                    ->order('sumnum DESC')->select();
                $this->assign('res',$res);
                $this->display();
            }
        }
}